from marshmallow import Schema, fields, validate, ValidationError, pre_load
from enums.AppEnum import AppEnum
from models import App, ScoringResult
from common.BaseResponse import BaseResponse
from common.ApiResponse import ApiResponse
from common.ErrorCode import ErrorCode


class queryFileResult(Schema):
    # 题目ID
    id = fields.Int(
        allow_none=True,
        required=False,
        validate=validate.Range(min=1, error="ID非法")
    )
    create_time = fields.Str(
        allow_none=True,
    )
    file_name = fields.Str(
        allow_none=True,
    )
    isIndex = fields.Int(
        allow_none=True,
        validate=validate.OneOf([0, 1])
    )
    # 必有值的数据
    pageSize = fields.Int(
        allow_none=True,
        validate=validate.Range(min=1)
    )
    current = fields.Int(
        allow_none=True,
        validate=validate.Range(min=1)
    )
    sortField = fields.Str(
        allow_none=True,
    )
    sortOrder = fields.Str(
        allow_none=True,
    )
    isDelete = fields.Int(
        required=True,
        allow_none=True,
        validate=validate.OneOf(
            [0, 1],
            error="isDelete数值非法！"
        )
    )

    @pre_load
    def replace_none_with_default(self, data, **kwargs):
        not_none_fields_with_default = {
            'pageSize': 10,
            'current': 1,
            'sortField': 'id',
            'sortOrder': 'asc',
            'isDelete': None,
        }
        for field, default_value in not_none_fields_with_default.items():
            if data.get(field) is None:
                data[field] = default_value
            return data
